package xin.basic.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xin.basic.model.Dict;
import xin.basic.model.DictGroup;
import xin.basic.service.DictService;
import xin.common.http.request.NamePageRequest;
import xin.common.http.response.PageResponse;
import xin.common.http.response.Response;

/**
 * <pre>
 * 数据字典
 * </pre>
 *
 * @author lixin_ma@outlook.com
 * @version 1.0
 * @since 2018/8/10
 */
@RequestMapping
@RestController
@Api(value = "xin-dict", description = "数据字典", position = 3)
public class DictController {

    @Autowired
    private DictService service;

    @ApiOperation(value = "数据字典列表" , notes = "已组为单位分页的",response = PageResponse.class )
    @GetMapping("dictGroup/pageList")
    public PageResponse eventPageList(NamePageRequest request){
        return service.findPageList(request);
    }

    @ApiOperation(value = "添加字典" , notes = "添加字典",response = Response.class )
    @PutMapping("dict")
    public Response putDict(@RequestBody Dict dict){
        return editDict(dict);
    }

    @ApiOperation(value = "编辑字典" , notes = "编辑字典",response = Response.class )
    @PostMapping("dict")
    public Response postDict(@RequestBody Dict dict){
        return editDict(dict);
    }

    @ApiOperation(value = "查看字典" , notes = "查看字典",response = Response.class )
    @GetMapping("dict/{dictId}")
    public Response getDict(@PathVariable String dictId){
        Response<Dict> response = new Response();
        response.setSuccess(Boolean.TRUE);
        response.setData(service.findDict(dictId));
        return response;
    }
    @ApiOperation(value = "删除字典" , notes = "删除字典",response = Response.class )
    @DeleteMapping("dict/del/{dictId}")
    public Response delDict(@PathVariable String dictId){
        Response<Dict> response = new Response();
        response.setSuccess(Boolean.TRUE);
        service.deleteDict(dictId);
        return response;
    }

    private Response editDict(Dict dict){
        Response<Dict> response = new Response();
        response.setSuccess(Boolean.TRUE);
        response.setData(service.saveDict(dict));
        return response;
    }

    @ApiOperation(value = "添加字典组" , notes = "添加字典组",response = Response.class )
    @PutMapping("dictGroup")
    public Response putDictGroup(@RequestBody Dict dict){
        return editDict(dict);
    }

    @ApiOperation(value = "编辑字典组" , notes = "编辑字典组",response = Response.class )
    @PostMapping("dictGroup")
    public Response postDictGroup(@RequestBody DictGroup dictGroup){
        Response<DictGroup> response = new Response();
        response.setSuccess(Boolean.TRUE);
        response.setData(service.saveDictGroup(dictGroup));
        return response;
    }

    @ApiOperation(value = "查看字典组" , notes = "查看字典组",response = Response.class )
    @GetMapping("dictGroup/{groupCode}")
    public Response getDictGroup(@PathVariable String groupCode){
        Response<DictGroup> response = new Response();
        response.setSuccess(Boolean.TRUE);
        response.setData(service.findDictGroup(groupCode));
        return response;
    }

    @ApiOperation(value = "删除字典组" , notes = "删除字典组",response = Response.class )
    @DeleteMapping("dictGroup/del/{groupCode}")
    public Response delDictGroup(@PathVariable String groupCode){
        Response<DictGroup> response = new Response();
        response.setSuccess(Boolean.TRUE);
        service.deleteDictGroup(groupCode);
        return response;
    }
}
